package cn.guxinhu.billsystem.user.controller;

import cn.guxinhu.billsystem.user.pojo.BillUser;
import cn.guxinhu.billsystem.user.service.impl.UserModifyServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Guxinhu
 * @create 2021-06-21 15:28
 */
@RestController
public class UserModifyController {
    /**
     * @Description: 修改的控制层
     * @Method: ModifyContrller.modify()
     * @Param: /modify -- 网页映射路径
     * user(User) -- 前端传入的实体类
     * @Return: mes(String)
     * @Author: Guxinhu
     * @Date Created in 2020/08/19 11:37
     */
    @Autowired
    private UserModifyServiceImpl userModifyService;

    @RequestMapping("/user/modify")
    public String modify(BillUser user) {
        String mes = "成功";
        if (user == null) {
            return "操作错误";
        }
        try {
            if (user.getPassWord().equals("") && user.getUserName().equals("")) {
                user.setUserName(null);
                user.setPassWord(null);
            }
            userModifyService.modify(user);
        } catch (Exception e) {
            DuplicateKeyException sqlException = e instanceof DuplicateKeyException ? ((DuplicateKeyException) e) : null;
            if (sqlException != null) {
                mes = "账号已存在";
            } else {
                mes = "操作失败";
            }
        }
        return mes;
    }
}
